package com.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/sessionServlet3")
public class SessionServlet3 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("执行了sessionServlet3...");

        //获取session对象
        HttpSession session = request.getSession();

        //向session域中存储数据
        session.setAttribute("user", "userid");

        //手动创建cookie，并且将底层自己创建的cookie给覆盖掉
        Cookie cookie = new Cookie("JSESSIONID", session.getId());

        //设置生存时长
        cookie.setMaxAge(60);

        //响应回去
        response.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
